import Vue from 'vue'
import Router from 'vue-router'
import Util from '@/assets/js/util';

Vue.use(Router)

const router =  new Router({
  routes: [
    {
      path: '*',
      redirect: '/login' //路由重定向
    },
    {
      path: '/login',
      name: 'login',
      component: resolve => require(['@/components/admin/login'], resolve)
    },
    {
      path: '/gameIndex',
      name: 'gameIndex',
      component: resolve => require(['@/view/game/gameTable/index'], resolve),
      meta: {
        parent: 'gameIndex',
        title: '微信游戏',
      }
    },
    {
      path: '/gameAdIndex',
      name: 'gameAdIndex',
      component: resolve => require(['@/view/game/gameAdTable/index'], resolve),
      meta: {
        parent: 'gameAdIndex',
        title: '游戏广告',
        // navs: [{label: '游戏广告', url: 'gameAdIndex'}, {label: '设置商品优惠'}]
      }
    },
    {
      path: '/gamePropIndex',
      name: 'gamePropIndex',
      component: resolve => require(['@/view/game/gamePropTable/index'], resolve),
      meta: {
        parent: 'gamePropIndex',
        title: '游戏道具',
      }
    },
    {
      path: '/gameChannelIndex',
      name: 'gameChannelIndex',
      component: resolve => require(['@/view/game/gameChannelTable/index'], resolve),
      meta: {
        parent: 'gameChannelIndex',
        title: '游戏渠道',
      }
    },
  ]
})

export default router;

router.afterEach((to) => {
  // 获取不到tagsList的时候先遍历处理tagsList；
  let vm = router.app;
  if (!vm.$store.state.tagsList.length) {
    let tagsList = [];
    router.options.routes.map((item) => {
      if (item.meta && item.meta.title) {
        tagsList.push(item);
      }
    });
    vm.$store.commit('setTagsList', tagsList);
  }
  Util.openNewPage(vm, to.name, to.params, to.query, to.meta.title);
  window.scrollTo(0, 0);
});
